import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'node:path';
import type { ServerResponse, IncomingMessage } from 'http';

export default defineConfig({
  plugins: [
    react(),
    {
      name: 'configure-content-types',
      configureServer(server) {
        server.middlewares.use((req: IncomingMessage, res: ServerResponse, next: () => void) => {
          // 为SVG文件设置正确的内容类型
          if (req.url?.endsWith('.svg')) {
            res.setHeader('Content-Type', 'image/svg+xml; charset=utf-8');
          } 
          // 为JavaScript文件设置更现代的内容类型
          else if (req.url?.endsWith('.js')) {
            res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
          } 
          // 为TypeScript文件设置内容类型
          else if (req.url?.endsWith('.ts') || req.url?.endsWith('.tsx')) {
            res.setHeader('Content-Type', 'application/typescript; charset=utf-8');
          }
          next();
        });
      }
    }
  ],
  optimizeDeps: {
    include: [
      'react',
      'react-dom',
      'react-router-dom',
      'lucide-react',
      '@emoji-mart/data',
      '@emoji-mart/react',
      'mitt',
      '@supabase/supabase-js',
      'axios',
      'js-cookie'
    ]
  },
  resolve: {
    dedupe: ['react', 'react-dom'],
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  server: {
    headers: {
      // 不设置全局Content-Type，让Vite根据文件类型自动设置
      // 'Content-Type': 'text/html; charset=utf-8',
      // 为开发环境设置缓存控制
      'Cache-Control': 'no-cache, max-age=0',
    }
  }
});